﻿namespace Fluid.SiteReader.Console
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Remoting;

    class Program
    {
        static IEnumerable<Type> SiteReaders
        {
            get { return typeof(Fluid.SiteReader.Client.Graph).Assembly.GetTypes().Where(type => type.IsSubclassOf(typeof(Fluid.SiteReader.Client.Graph))); }
        }

        static void Main(string[] args)
        {
            try
            {
#if DEBUG
                //string url = "dd456779";
                //string url = "ms733764";
                string url = "ms731190";
                new Fluid.SiteReader.Client.Msdn(url).Fetch().WritePdf();
#else
                if (args.Length >= 2 && !string.IsNullOrWhiteSpace(args[0]) && !string.IsNullOrWhiteSpace(args[1]))
                {
                    string fileName = (args.Length == 3) ? args[2] : null;
                    Type runtimeType = SiteReaders.Where(t => string.Equals(t.FullName, "Fluid.SiteReader.Client." + args[0], StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                    if (runtimeType != null)
                        (Activator.CreateInstance(runtimeType, args[1]) as Fluid.SiteReader.Client.IGraph).Fetch().WritePdf(fileName);
                }
                else
                    Console.WriteLine("Usage: Fluid.SiteReader.Console <site> <pagename> [export filename]");
#endif
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
            }
        }

    }
}
